package cn.daniellee.plugin.otp.command;

import cn.daniellee.plugin.otp.OptimizedTeleport;
import cn.daniellee.plugin.otp.menu.PlayerMenu;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.scheduler.BukkitRunnable;

public class TeleportCommand implements CommandExecutor {


	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
		if ("otpreload".equalsIgnoreCase(command.getName())) {
			OptimizedTeleport.getInstance().reloadConfig();
			OptimizedTeleport.getInstance().loadConfig();
			commandSender.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("reload-success", "&eConfiguration reload completed.")).replace("&", "§"));
			return true;
		}
		if (!(commandSender instanceof Player)) {
			commandSender.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("console-cant-run", "&eThe console cannot execute this command.")).replace("&", "§"));
			return true;
		}
		Player playerA = (Player) commandSender;
		//处理传送请求
		if ("otpa".equalsIgnoreCase(command.getName()) || "otpahere".equalsIgnoreCase(command.getName())) {
			if(strings.length != 1) {
				Inventory inventory = PlayerMenu.generate(playerA.getName(), 1);
				playerA.openInventory(inventory);
				return true;
			}
			Player playerB = Bukkit.getPlayer(strings[0]);
			if (playerA.equals(playerB)) {
				playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("request-oneself", "&eYou can't request to teleport yourself.")).replace("&", "§"));
				return true;
			}
			if (playerB == null) {
				playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("player-offline", "&eTarget player is not online.")).replace("&", "§"));
				return true;
			}
			if(OptimizedTeleport.getInstance().getRequests().get(playerB.getName()) != null) {
				playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("already-requested", "&b{name}&e has pending teleport request, please try again later.").replace("{name}", playerB.getName())).replace("&", "§"));
				String retryText = OptimizedTeleport.getInstance().getConfig().getString("retry", "&eClick me to send the request again to &b{name}").replace("{name}", playerB.getName());
				TextComponent retry = new TextComponent((OptimizedTeleport.getInstance().getPrefix() + retryText).replace("&", "§"));
				retry.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/" + command.getName() + " " + playerB.getName()));
				retry.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(retryText).color(ChatColor.BLUE).create()));
				playerA.spigot().sendMessage(retry);
				return true;
			}
			playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("request-send", "&eRequest has been sent to &b{name}").replace("{name}", playerB.getName())).replace("&", "§"));
			playerB.sendMessage(("&6&m&l|&a&m&l---&8&m&l---------&7&l[&b" + OptimizedTeleport.getInstance().getConfig().getString("prompt-prefix") + "&7&l]&8&m&l----------&a&m&l---&6&m&l|").replace("&", "§"));
			TextComponent accept = new TextComponent(("&7[&a" + OptimizedTeleport.getInstance().getConfig().getString("accept", "&aAccept") + "&7]   &m&l| " + ("otpa".equalsIgnoreCase(command.getName()) ? OptimizedTeleport.getInstance().getConfig().getString("request-tpa", "&b{name} &erequest to teleport to your location") : OptimizedTeleport.getInstance().getConfig().getString("request-tpahere", "&b{name} &einvites you to teleport to his location")).replace("{name}", playerA.getName())).replace("&", "§"));
			accept.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/otpaccept"));
			accept.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(OptimizedTeleport.getInstance().getConfig().getString("accept", "&aAccept").replace("&", "§")).color(ChatColor.BLUE).create()));
			playerB.spigot().sendMessage(accept);
			TextComponent deny = new TextComponent(("&7[&a" + OptimizedTeleport.getInstance().getConfig().getString("refuse", "&cRefuse") + "&7]   &m&l| &7" + OptimizedTeleport.getInstance().getConfig().getString("request-refuse", "&eClick me refuse to send the request")).replace("&", "§"));
			deny.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/otpdeny"));
			deny.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(OptimizedTeleport.getInstance().getConfig().getString("refuse", "&cRefuse").replace("&", "§")).color(ChatColor.BLUE).create()));
			playerB.spigot().sendMessage(deny);
			playerB.sendMessage(("&6&m&l|&a&m&l--------------------------------&6&m&l|").replace("&", "§"));
			new BukkitRunnable() {
				int time = OptimizedTeleport.getInstance().getConfig().getInt("timeout");
				@Override
				public void run() {
					OptimizedTeleport.getInstance().getRequests().putIfAbsent(playerB.getName(), "Executed");
					if("Accepted".equals(OptimizedTeleport.getInstance().getRequests().get(playerB.getName()))) {
						if ("otpa".equalsIgnoreCase(command.getName())) {
							playerA.teleport(playerB.getLocation());
						} else {
							playerB.teleport(playerA.getLocation());
						}
						playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("accepted", "&b{name} &ehas accepted your request.").replace("{name}", playerB.getName())).replace("&", "§"));
						OptimizedTeleport.getInstance().getRequests().remove(playerB.getName());
						cancel();
					} else if("Refused".equals(OptimizedTeleport.getInstance().getRequests().get(playerB.getName()))) {
						playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("refused", "&b{name} &erejected your request.").replace("{name}", playerB.getName())).replace("&", "§"));
						OptimizedTeleport.getInstance().getRequests().remove(playerB.getName());
						cancel();
					} else if(time == 0) {
						String timeout = (OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("request-timeout", "&ePending teleport request has expired.")).replace("&", "§");
						playerA.sendMessage(timeout);
						playerB.sendMessage(timeout);
						OptimizedTeleport.getInstance().getRequests().remove(playerB.getName());
						cancel();
					}
					time--;
				}
			}.runTaskTimer(OptimizedTeleport.getInstance(), 0L, 20);
		} else if ("otpaccept".equalsIgnoreCase(command.getName())) {
			if(OptimizedTeleport.getInstance().getRequests().get(playerA.getName()) == null) {
				playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("no-requests", "&eYou have no pending requests.")).replace("&", "§"));
			} else {
				OptimizedTeleport.getInstance().getRequests().put(playerA.getName(), "Accepted");
				playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("target-accepted", "&eYou have accepted the teleport request.")).replace("&", "§"));
			}
		} else if ("otpdeny".equalsIgnoreCase(command.getName())) {
			if(OptimizedTeleport.getInstance().getRequests().get(playerA.getName()) == null) {
				playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("no-requests", "&eYou have no pending requests.")).replace("&", "§"));
			} else {
				OptimizedTeleport.getInstance().getRequests().put(playerA.getName(), "Refused");
				playerA.sendMessage((OptimizedTeleport.getInstance().getPrefix() + OptimizedTeleport.getInstance().getConfig().getString("target-refused", "&eYou declined the teleport request.")).replace("&", "§"));
			}
		} else if ("otphelp".equalsIgnoreCase(command.getName())) {
			sendHelp(playerA);
		}
		return true;
	}

	private void sendHelp(Player player) {
		player.sendMessage(("&m&a----&m&6----------&3 " + OptimizedTeleport.getInstance().getConfig().getString("prompt-prefix", "OptimizedTeleport") + " &m&6----------&m&a----").replace("&", "§"));

		String tpaText = OptimizedTeleport.getInstance().getConfig().getString("tpa-help", "&eRequest to be sent to a player (/tpa)").replace("&", "§");
		TextComponent tpaHelp = new TextComponent("/otpa <player>  " + tpaText);
		tpaHelp.setColor(ChatColor.GRAY);
		tpaHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/otpa "));
		tpaHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(tpaText).color(ChatColor.BLUE).create()));
		player.spigot().sendMessage(tpaHelp);

		String tpacceptText = OptimizedTeleport.getInstance().getConfig().getString("tpaccept-help", "&eAccept the teleport request (/tpaccept)").replace("&", "§");
		TextComponent tpacceptHelp = new TextComponent("/otpaccept  " + tpacceptText);
		tpacceptHelp.setColor(ChatColor.GRAY);
		tpacceptHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/otpaccept"));
		tpacceptHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(tpacceptText).color(ChatColor.BLUE).create()));
		player.spigot().sendMessage(tpacceptHelp);

		String tpdenyText = OptimizedTeleport.getInstance().getConfig().getString("tpdeny-help", "&eRefuse to teleport request (/tpdeny)").replace("&", "§");
		TextComponent tpdenyHelp = new TextComponent("/otpdeny  " + tpdenyText);
		tpdenyHelp.setColor(ChatColor.GRAY);
		tpdenyHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/otpdeny"));
		tpdenyHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(tpdenyText).color(ChatColor.BLUE).create()));
		player.spigot().sendMessage(tpdenyHelp);

		String tpahereText = OptimizedTeleport.getInstance().getConfig().getString("tpahere-help", "&eRequest to teleport a player to your location (/tpahere)").replace("&", "§");
		TextComponent tpahereHelp = new TextComponent("/otpahere <player>  " + tpahereText);
		tpahereHelp.setColor(ChatColor.GRAY);
		tpahereHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/otpahere "));
		tpahereHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(tpahereText).color(ChatColor.BLUE).create()));
		player.spigot().sendMessage(tpahereHelp);
	}
}
